package service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import models.BaseEntity.Status;
import models.Order;
import models.Order.OrderStatus;
import models.OrderItem;
import models.ShoppingCar;
import models.User;

public class OrderService {
	/**
	 * 把购物车转换为订单项。
	 * @param shoppingCars 购物车里的商品
	 * @param editer  操作者
	 * @return
	 */
	public static List<OrderItem> convert2OrderItems(List<ShoppingCar> shoppingCars, Order order, User editer) {
		if(shoppingCars == null) return null;
		List<OrderItem> items = new ArrayList<OrderItem>();
		for(ShoppingCar sCar : shoppingCars) {
			OrderItem item = new OrderItem();
			item.setCreator(editer);
			item.resetModifier(editer);
			//计算价格。
			CommoditiesService.setCommodityPriceByUser(sCar.commodity, sCar.user);
			item.quantity = sCar.quantity;
			item.price = sCar.commodity.price;
			item.cost = sCar.commodity.cost;
			item.weightPerUnit = sCar.commodity.weight;
			item.remark = sCar.remark;
			item.commodity = sCar.commodity;
			item.order = order;
			items.add(item);
		}
		return items;
	}
	
	/**
	 * 订单价格计算。
	 */
	public static void caculateOrder(Order order) {
		if(order == null || order.orderItems == null) return;
		//1 初始化参数。
		//========初始参数   START===================================
		/*货物起始公斤数，超出的费用按每公斤递增。*/
		BigDecimal baseFreightKG = new BigDecimal(order.baseFreightKG);
		/*货物起始运费。*/
		BigDecimal baseFreight = new BigDecimal(order.baseFreight);
		/*每递增1公斤运费*/
		BigDecimal freightPerKG = new BigDecimal(order.freightPerKG);
		//========初始参数   END======================================
		
		//========计算得出的参数   START===================================
		//商品总价格。
		BigDecimal commodityPrice = new BigDecimal(0);
		//商品总重量。
		BigDecimal totalWeight = new BigDecimal(0);
		//商品总运费。
		BigDecimal totalFreight = new BigDecimal(0);
		//订单总价格。
		BigDecimal totalPrice = new BigDecimal(0);
		//========计算得出的参数   END===================================
		//2 计算订单项总结。
		for(OrderItem item : order.orderItems) {
			BigDecimal quantity = new BigDecimal(item.quantity);
			BigDecimal price = new BigDecimal(item.price);
			BigDecimal weightPerUnit = new BigDecimal(item.weightPerUnit);
			commodityPrice = commodityPrice.add(quantity.multiply(price));
			totalWeight = totalWeight.add(quantity.multiply(weightPerUnit));
		}
		//3 计算运费。
		totalFreight = baseFreight;
		if(totalWeight.compareTo(baseFreightKG) > 0) {
			BigDecimal overKGDouble = totalWeight.subtract(baseFreightKG);
			BigDecimal overKGLong = new BigDecimal(overKGDouble.intValue());
			totalFreight = totalFreight.add(freightPerKG.multiply(overKGLong));
			if(overKGLong.compareTo(overKGDouble) < 0) {
				totalFreight = totalFreight.add(freightPerKG);
			}
		}
		//4 计算总价格。
		totalPrice = totalFreight.add(commodityPrice);
		//5 设置值。
		order.commodityPrice = commodityPrice.doubleValue();
		order.totalWeight = totalWeight.doubleValue();
		order.totalFreight = totalFreight.doubleValue();
		order.totalPrice = totalPrice.doubleValue();
	}
	
	/**
	 * 保存订单。
	 * @param order
	 */
	public static void create(Order order, User editor) {
		order.setCreator(editor);
		order.resetModifier(editor);
		order.orderStatus = OrderStatus.NEW;
		order.status = Status.ENABLE;
		OrderService.caculateOrder(order);
//		List<OrderItem> items = order.orderItems;
//		order.orderItems = null;
		order.save();
//		for(OrderItem item : items) {
//			item.order = order;
//			item.save();
//		}
	}
	
	/**
	 * 删除整张订单。
	 * @param order
	 * @param editor
	 */
	public static void delete(Order order) {
		if(order != null) {
			OrderItem.delete("order.id=?", order.id);
			Order.delete("id=?", order.id);
		}
	}
}
